package com.morgan.basemodule.fragment

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.ViewModelProvider
import com.morgan.basemodule.utils.GsonTypeToken
import com.morgan.basemodule.view.IBaseView
import com.morgan.basemodule.viewmodel.BaseVM

/**
 *description：框框Fragment
 *<br>author：caowugao
 *<br>time：2021/4/5 0005 16:13
 */
open abstract class FrameFragment<BINDING : ViewDataBinding, MODEL : BaseVM> : VisibilityFragment(), IBaseView {

    protected lateinit var mBinding :BINDING
    protected lateinit var mModel : MODEL
    private var mFirstLoaded = false

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        val layoutId = getLayoutId()
        if (layoutId != null && layoutId != 0 && layoutId != -1) {
            mBinding = DataBindingUtil.inflate(inflater, layoutId, container, false)
        } else {
            val layoutView = getLayoutView()
            if (layoutView != null) {
                mBinding = DataBindingUtil.bind<BINDING>(layoutView)!!
            }
        }

        mBinding.lifecycleOwner = this
        val variableId = getVariableId()
        val variableModel = getVariableModel()
        mModel = variableModel
        variableModel.attachUI(this)
        if (variableId != null && variableId != 0 && variableId != -1) {
            mBinding.setVariable(variableId, variableModel)
        }

        return mBinding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        initView(view)
        initData(savedInstanceState)
    }

    protected open fun initData(savedInstanceState: Bundle?) {
        mModel.clickEvent.observe(viewLifecycleOwner) {v ->
            onClick(v)
        }
    }

    protected open fun initView(view: View) {

    }

    open fun getVariableModel(): MODEL = ViewModelProvider(this).get(getModelClass(1))

    /**
     * index:表示该泛型参数在类的位置。比如 A<T,E..>.从0开始
     */
    protected fun getModelClass(index : Int) : Class<MODEL> = GsonTypeToken.getSuperclassTypeParameterClass(javaClass, index)!!


    abstract fun getVariableId(): Int?

    open fun getLayoutView(): View? = null

    abstract fun getLayoutId(): Int?

    override fun showLoading() {
    }

    override fun hideLoading() {
    }

    override fun showEmpty() {
    }

    override fun showError() {
    }

    override fun showNetError() {
    }

    override fun getLifecycleOwner(): LifecycleOwner = this


    override fun onClick(v: View?) {
    }

    override fun onLongClick(v: View?): Boolean = false


    override fun onItemClick(position: Int, bean: Any?) {
    }

    override fun onItemLongClick(position: Int, bean: Any?) {
    }

    override fun onVisibilityChanged(visible: Boolean) {
        super.onVisibilityChanged(visible)
        if (!mFirstLoaded && visible) {
            mFirstLoaded = true
            onLazyLoad()
        }
    }

    /**
     * 懒加载，在用户可见时 只会调用一次。initData(savedInstanceState) 不能请求网络数据， 可以在这里再进行请求网络数据
     */
    protected open fun onLazyLoad() {
        
    }
}